<template>
  <div>
    <a-table
      :columns="columns"
      :data-source="alarms"
      :rowKey="(record, index) => index"
      size="small"
    >
      <span slot="opertion" slot-scope="text, record" v-if="showOperation">
        <a @click="forceClear(record)">强制消除</a>
      </span>
    </a-table>
  </div>
</template>
<script>
var moment = require("moment");
moment.defaultFormat = "YYYY-MM-DD HH:mm:ss";
const columns = [
  {
    title: "设备",
    dataIndex: "thing_name",
  },
  {
    title: "设备id",
    dataIndex: "t_id",
  },
  {
    title: "站点",
    dataIndex: "station_name",
  },
  {
    title: "告警",
    dataIndex: "alarm_name",
  },

  {
    title: "索引",
    dataIndex: "mindex",
  },
  {
    title: "级别",
    dataIndex: "level",
  },
  {
    title: "告警时间",
    dataIndex: "alarm_time",
  },
  {
    title: "消除时间",
    dataIndex: "clear_time",
  },
  {
    title: "告警原因",
    dataIndex: "params['reason']",
  },
  {
    title: "相关量",
    dataIndex: "properties",
  },
  {
    title: "操作",
    dataIndex: "opertion",
    scopedSlots: { customRender: "opertion" },
  },
];
import {forceAalarm} from '@/services/alarmAPI'
export default {
  data() {
    return {
      columns,
    };
  },
  props: ["alarms", "showOperation"],
   watch: {
    alarms: {
      handler(alarms) {
        console.log("alarm changed---------------------------------");
        if (alarms && Array.isArray(alarms)) {
          let sname = new Set();
          let ssname = new Set();
          let sdname = new Set();

          alarms.forEach((alarm) => {
            if(typeof alarm.alarm_obj == 'string' ){
              alarm.alarm_obj = JSON.parse(alarm.alarm_obj)
              alarm.alarm_time = alarm.alarm_obj.time
              alarm.clear_time = alarm.alarm_obj.clear_time
            }
            if(alarm.time){
              alarm.alarm_time = alarm.time
            }
            alarm.alarm_time = moment(alarm.alarm_time * 1000).format()
            if (alarm.flag == "END"){
              alarm.clear_time = moment(alarm.clear_time * 1000).format()
            }
            else{
              alarm.clear_time = "-"
            }
            
            sname.add(alarm.alarm_name);
            if (alarm.station_name) {
              ssname.add(alarm.station_name);
            }
            sdname.add(alarm.thing_name);
            let oalarm = alarm.alarm_obj;
            if (oalarm && oalarm.params) {
             
              alarm.reason = oalarm.params.reason
              //return alarm;
            }

            //alert("nodata");
            //return alarm;
          });
          
        }
      },
    },
  },
  methods: {
    forceClear(alarm) {
      forceAalarm(alarm.t_id,alarm.alarm_id,0,"END")
    },
  },
};
</script>
